package com.lzhpo.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.lzhpo.core.domain.PrizeData;
import com.lzhpo.core.domain.ResponseObj;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author <a href="lijing1@wxchina.com@wxchina.com">Lijin</a>
 * @Description TODO
 * @Date 2019/12/19 14:55
 * @Version 1.0
 **/
public class DataGeneratorUtil {

    public static final FastDateFormat fastDateFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
    public static final String[] data = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10"};
    public static final FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
    public static final FastDateFormat formatYmd = FastDateFormat.getInstance("yyyy-MM-dd");

    private static Logger logger = LoggerFactory.getLogger(MyDateUtil.class);


    /**
     * 随机产生 01 ---10 的10个数
     */

    public static String generate() {
        List<Integer> set = Lists.newArrayList();
        List<String> result = null;
        while (true) {
            if (set.size() == 10) {
                break;
            }
            int num = new Random().nextInt(10);
            if (!set.contains(num)) {
                set.add(num);
            }
        }
        result = set.stream().map(s -> data[s]).collect(Collectors.toList());
        return StringUtils.collectionToCommaDelimitedString(result);
    }


    public static PrizeData converStrToPrizeData(String data) {
        String[] arr = data.split(";");
        PrizeData prizeData = new PrizeData();
        prizeData.setTermNo(arr[0]);
        String prizeNums = arr[1];
        String[] nums = StringUtils.commaDelimitedListToStringArray(prizeNums);
        prizeData.setPrizeNums(nums);
        try {
            prizeData.setOpenTIme(format.parse(arr[2]));
        } catch (ParseException e) {
            e.printStackTrace();
            logger.error("converStrToPrizeData error ", e);
        }
        return prizeData;
    }

    /**
     * [{"issue":"743924","opendate":"2020-01-14 13:30:00","code":"1,3,7,8,10,9,4,5,2,6",
     * "lotterycode":"BJPK10","officialissue":"743924"}]
     */
    public static PrizeData parseJsonStrToPrizeData(String bodyStr, DataTypeEnum type) {
        PrizeData data = new PrizeData();
        try {
            List<ResponseObj> list = JSON.parseArray(bodyStr, ResponseObj.class);
            if (CollectionUtils.isNotEmpty(list)) {
                ResponseObj obj = list.get(0);
                data.setTermNo(obj.getIssue());
                data.setPrizeNums(decorateCode(obj.getCode()));
                data.setType(String.valueOf(type.getType()));
                data.setOpenTIme(fastDateFormat.parse(obj.getOpendate()));

            }
        } catch (ParseException e) {
            e.printStackTrace();
            logger.error("response parse error {}  bodystr is {}", e, bodyStr);
        }
        return data;
    }

//[{"issue":"20200603-072","opendate":"2020-06-03 19:04:00","code":"10,7,6,3,8,9,1,4,2,5","lotterycode":"XIYFT","officialissue":"20200603-072"}]
    public static PrizeData parseJsonArrayToPrizeData(String str,DataTypeEnum typeEnum){
        JSONArray arr=JSONArray.parseArray(str);
        JSONObject jsonObject=arr.getJSONObject(0);
        PrizeData data = new PrizeData();
        data.setTermNo(jsonObject.getString("issue"));
        data.setPrizeNums(decorateCode(jsonObject.getString("code")));
        data.setType(typeEnum.getType()+"" );
        try {
            data.setOpenTIme(fastDateFormat.parse(jsonObject.getString("opendate")));
        } catch (ParseException e) {
            e.printStackTrace();
            data.setOpenTIme(new Date());
        }
        return data;
    }

    public static PrizeData parseJsonStrToPrizeData(List<ResponseObj> list,
                                                    DataTypeEnum type) {
        PrizeData data = new PrizeData();
        if (CollectionUtils.isNotEmpty(list)) {
            ResponseObj obj = list.get(0);
            data.setTermNo(obj.getIssue());
        //    data.setPrizeNums(decorateCode(obj.getCode()));
            data.setPrizeNums(obj.getCode().split(","));
            data.setType(String.valueOf(type.getType()));
            try {
                data.setOpenTIme(fastDateFormat.parse(obj.getOpendate()));
            } catch (ParseException e) {
                e.printStackTrace();
                data.setOpenTIme(new Date());
            }
        }
        return data;
    }

    private static String[] decorateCode(String code) {
        String[] arr = code.split(",");
        return Stream.of(arr).map(s -> {
            if (Integer.valueOf(s) < 10 && !s.startsWith("0")) {
                return "0" + s;
            } else {
                return s;
            }
        }).collect(Collectors.toList()).toArray(new String[arr.length]);
    }


    public static PrizeData tranJsonToPrizeData(JSONObject jsonObjectParam) {
        JSONArray array=jsonObjectParam.getJSONArray("data");
        JSONObject jsonObject  =array.getJSONObject(0);
        PrizeData data=new PrizeData();
        data.setTermNo(jsonObject.getString("expect"));
        String [] arr=jsonObject.getString("opencode").split(",");
        String [] numArr=addEleToArr(arr);
        data.setPrizeNums(numArr);
        data.setType(String.valueOf(DataTypeEnum.ARRANGE_FIVE.getType()));
        try {
            data.setOpenTIme(fastDateFormat.parse(
                    jsonObject.getString("opentime")));
        } catch (ParseException e) {
            e.printStackTrace();
            data.setOpenTIme(new Date());
        }

        return data;
    }


    private static String[] addEleToArr(String[] arr) {
        String[] arrTemp=new String[10];
        int length=arr.length;
        for (int i = 0; i < arrTemp.length; i++) {
            if (i<length){
                arrTemp[i]=arr[i];
            }else {
                arrTemp[i]="0";
            }
        }
        return arrTemp;
    }
}
